<?php
class globalModel{
	public $db = null;
	public function __construct() {
		$this->db = &JFactory::getDBO();
	}

	public function getFirstImgURL($content) {
		$newStr = "";
		$signal = 'src=';
		$pos = strpos($content,$signal);
		$newStr = substr($content, $pos + 5);
		$pos = strpos($newStr,'"');
		$newStr = substr($newStr, 0, $pos);
		return $newStr;
	}
	public function getArticle($id) {
		$query = "select * from #__content where id = '{$id}' AND state=1";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas[0];
	}
	
	public function getOrderById($id) {
		$query = "select a.*,b.* from jos_vm_orders a, jos_vm_order_item b  where (a.order_id = b.order_id) AND (a.order_id = '{$id}')";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
	
	public function getUserInfFromTelefon($telefon) {
		$query = "select * from #__vm_user_info where 	phone_1 = '{$telefon}' ORDER BY user_id DESC";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas[0];
	}
	
	public function getCategories($id) {
		$query = "select * from #__categories where id = '{$id}' AND published=1";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas[0];
	}
	
	public function getMWCItemId($catId) {
		$query = "select str_link from #__mwc_itemid where cat_id = '{$catId}'";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas[0];
	}
	
	public function getImageFromContentId($contentId) {
		$query = "select * from #__content where id = '{$contentId}'";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas[0];
	}
	
	public function getArticleByCatId($catId, $contentId = null) {
		$query = "select * from #__content where catid = '{$catId}' AND (state=1)";
		if ( $contentId != null )
			$query .= " AND id <> ".$contentId;
		$query .= " ORDER BY ordering ASC";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
	
	public function deleteArticle($id) {
		$query = "delete from #__content where id = '{$id}'";
		return $this->db->execute($query);
	}
	
	public function getIntroduce()
	{
		$ar = $this->getArticle(49);
		$ar['introtext'] = $this->getShortText($ar['introtext'], 1000);
		return $ar;
	}
	
	public function getProductCategories()
	{
		$query = "select * from #__mwc_categories order by ordering ASC";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}	
	
	public function getProductSpecial()
	{
		$query = "select a.*, b.category_id from #__vm_product a, #__vm_product_category_xref b 
					WHERE (product_special = 'Y') AND (a.product_id = b.product_id) AND (a.product_publish='Y')
					GROUP BY a.product_id 
					order by product_order_levels ASC LIMIT 0,6";	
			
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}	
	
	public function getCategoryName($id) {
		$query = "select * from #__vm_category where category_id = '{$id}'";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas[0];
	}
	
	public function getProducts()
	{
		$query = "SELECT * FROM #__vm_product ORDER BY product_id DESC LIMIT 0 , 2";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
	
	public function getMWCBanners($type=0,$id = null)
	{
		$query = "SELECT * FROM #__mwc_banner ";
		
		$query .= " WHERE type=".$type;
		if ( $id > 0 )
			$query .= " AND id = {$id} ";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
	
	public function getOptionsDetail($id)
	{
		$query = "select * from #__mwc_options_detail where option_id = '{$id}' order by ordering ASC";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}	

//	public function getData()
//	{
//		$query = "select product_id from #__vm_product_price";
//		$this->db->setQuery( $query );
//		$datas = $this->db->loadAssocList();
//		return $datas;
//	}	
//	public function getDataP($strId)
//	{
//		$query = "select product_id from #__vm_product where product_id NOT IN (".$strId.")";
//		$this->db->setQuery( $query );
//		$datas = $this->db->loadAssocList();
//		return $datas;
//	}	
//	
//	public function insert($iId)
//	{
//		$query = "insert into #__vm_product_price  set product_id=$iId, 
//		product_price='0.00000',product_currency='DKK',product_price_vdate=0,
//		product_price_edate=0,cdate=".time().",mdate=".time().",shopper_group_id=5,
//		price_quantity_start=0,price_quantity_end=0
//		";
//	
//		$this->db->execute( $query );
//		
//	}	
	
	public function getOptionsDetailFromId($id)
	{
		$query = "select * from #__mwc_options_detail where id = '{$id}' order by ordering ASC";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas[0];
	}		
	
	public function getShortText($text, $startNum = 40) {
//		$text = strip_tags($text);
		$text = strip_tags($text,'<strong><br>');
		if(strlen($text) <= $startNum) {
			return $text;
		}
		$char = "p";
		while($char != " ") {
			$char = substr($text,$startNum, 1);
			$startNum++;
		}
		return substr($text,0, $startNum);
	}
	
	public function isHomePage()
	{
		$option = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');

		if ($option == 'com_content' && $view == 'frontpage') { 
			return true;
		} else {
			return false;
		}
	}	
	
	public function isKontaktPage()
	{
		$option = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');

		if ($option == 'com_kontakt') { 
			return true;
		} else {
			return false;
		}
	}	
	
	public function isOmOsPage()
	{
		$option = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		$layout = JRequest::getCmd('layout');

		if (($option == 'com_content') && ($view='article') && ($layout='omos')) { 
			return true;
		} else {
			return false;
		}
	}	
		
}
//-- code insert data
//$strData = $globalModel->getData();
//$str = '';
//
//foreach($strData as $key=>$value){
//	if ( !empty($str) )
//		$str .=','.$value['product_id'];
//	else 
//		$str .= $value['product_id'];
//}
//
//$dataP = $globalModel->getDataP($str);
//
//foreach($dataP as $key=>$value){
//	if ( !empty($str) )
//		$str .=','.$value['product_id'];
//	else 
//		$str .= $value['product_id'];
//}
//
//
//
//foreach($dataP as $key=>$value ){
//	$globalModel->insert($value['product_id']);
//}
?>